package com.yuntongxun.ecdemo.voicechat.ui.activity;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.yuntongxun.ecdemo.R;
import com.yuntongxun.ecdemo.voicechat.bean.PostResult;
import com.yuntongxun.ecdemo.voicechat.comman.EventBusTags;

import java.io.IOException;
import java.io.InputStream;

import butterknife.InjectView;
import butterknife.OnClick;

import static com.yuntongxun.ecdemo.voicechat.ui.activity.RegisterFlagActivity.FLAG;

/**
 * 注册页面2-选择性别
 *
 * @author li_zhuonan
 * @version 1.0
 * @since 2016/11/15
 */
public class RegisterGenderActivity extends BaseChatActivity {

    public static final String GENDER = "gender";
    public static final String MAN = "男";
    public static final String WOMAN = "女";
    private String gender="";

    @InjectView(R.id.country_img)
    ImageView flagImg;
    @InjectView(R.id.country_name_tv)
    TextView flagNameTv;
    @InjectView(R.id.man_check)
    CheckBox manCheck;
    @InjectView(R.id.woman_check)
    CheckBox womanCheck;

    @Override
    protected boolean isBindEventBusHere() {
        return true;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_voice_chat_register2;
    }

    @Override
    protected void initViewsAndEvents() {
        setTitle("Register");
        initData();
        initCheckBox();
    }

    private void initData(){
        String flagName = getIntent().getStringExtra(FLAG);
        Log.e("flag", flagName + "");
        try {
            InputStream is = getAssets().open("flag/" + flagName + ".png");
            flagImg.setImageBitmap(BitmapFactory.decodeStream(is));
            flagNameTv.setText(flagName);
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void initCheckBox(){
        manCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    womanCheck.setChecked(false);
                }else{
                    womanCheck.setChecked(true);
                }

            }
        });

        womanCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    manCheck.setChecked(false);
                }else{
                    manCheck.setChecked(true);
                }

            }
        });

    }

    @OnClick(R.id.next_btn)
    public void onNextClick(){
        if (manCheck.isChecked()){
            gender=MAN;
        }

        if (womanCheck.isChecked()){
            gender=WOMAN;
        }

        Intent intent=new Intent(this,RegisterAgeActivity.class);
        intent.putExtra(FLAG,flagNameTv.getText().toString().trim());
        intent.putExtra(GENDER,gender);
        startActivity(intent);
    }

    public void onEvent(PostResult postResult){
        if (EventBusTags.REGISTER_FINISH.equals(postResult.getTag())){
            finish();
        }
    }




}
